/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2021-10-30 10:40:38
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2021-10-31 22:32:14
 */
import request from '@/utils/request'
import Layout from '@/layout'
// 其他子页面中间件

export function getMenus(token) {
  return request({
    url: '/system/index/menus',
    method: 'get'
  })
}

export function setBlocCache(data) {
  return request({
    url: 'system/settings/set-cache',
    method: 'post',
    data
  })
}

export function getCitylist() {
  return request({
    url: '/map/citylist',
    method: 'get'
  })
}

export function loadFile(path) {
  return (resolve) => require(['@/views' + path], resolve)

  // if (path.indexOf('vue') > -1) {
  //   return () => import('@/views' + path)
  // } else {
  // }
}

export function initMenus(menus) {
  menus.forEach((item, index) => {
    // 如果是第一级别的菜单 并且子菜单不为空，name给一级菜单设置全局布局
    switch (item.level_type) {
      case 1:
        // 一级菜单
        item.component = Layout
        // console.log('一2级菜单1 ', item)

        if (item.children.length === 1) {
          // item.meta = null
          // index
          item.redirect = item.children[0].path
          // item.alwaysShow = false
          // delete item.meta
          delete item.name
          // console.log('一2级菜单2 ', item.children)
        }
        // console.log('一2级菜单2 ', item.name, item)
        break
      case 2:
        // 一级菜单无二级
        item.component = Layout // loadFile(item.path)
        // 增加二级菜单且不显示，用于跳转
        // item.children.push(item)
        break
      case 3:
        // 二级菜单
        item.component = loadFile(item.path)
        break
      case 4:
        // 二级菜单无三级
        item.component = loadFile(item.path)
        break
      case 5:
        // 三级菜单,三级菜单下不能包含子页面菜单，只能包含非页面菜单
        item.component = loadFile(item.path)
        break
      case 6:
        // 非菜单页面
        // console.log('layout 加载', item, index, menus[index])
        // 找到父级的父级
        item.component = loadFile(item.path)
        break
      default:
        // 默认一级菜单
        item.component = Layout
        break
    }

    if (item.children.length > 0) {
      initMenus(item.children)
    }

    return item
  })

  return menus
}

export function initLeftMenus(menus) {
  const leftMenu = []
  menus.forEach((item, index) => {
    // 如果是第一级别的菜单 并且子菜单不为空，name给一级菜单设置全局布局
    if (item.parent === 0) {
      leftMenu.push(item)
    }
  })
  return leftMenu
}

// 初始化菜单
export function getMenuRoutes(menus) {
  const menuRoutes = initMenus(menus)
  buildMenuRoutesStep2(menuRoutes)
  // console.log(JSON.stringify(menuRoutes))
  return menuRoutes
}

// 处理非菜单页面
function buildMenuRoutesStep2(routes, root) {
  let list = []
  return routes.filter(its => {
    if (its.level_type === 6) {
      // root = root.concat(its)
      // 过滤掉非页面菜单
      root.children.forEach((chi, index) => {
        if (chi.id !== its.id) {
          delete root.children[index]
        }
      })
      root.children = root.children.filter(chi => chi.id !== its.id)
    } else {
      // 过滤孙子辈
      list = its.children.filter(son => {
        if (son.children) {
          const sonl = son.children.filter(sonchi => sonchi.level_type === 6)
          if (sonl.length > 0) {
            return sonl
          } else {
            return false
          }
        } else {
          return false
        }
      })

      if (list.length > 0) {
        list.forEach((l, s) => {
          l.children.forEach(m => {
            its.children.push(m)
          })
        })
      }
    }
    root = its.level_type !== 6 ? its : root
    buildMenuRoutesStep2(its.children, root)
  })
}
